from dataclasses import dataclass, field
import os

"""
Options:
  -o, --output <file name>     saves the generated script to a file  将生成的脚本保存到文件中
  --target <language>          language to generate, one of javascript, test, python, python-async, csharp (default: "python") 生成的脚本语言，默认是python
  -b, --browser <browserType>  browser to use, one of cr, chromium, ff, firefox, wk, webkit (default: "chromium")  选择指定浏览器，默认是chromium
  --channel <channel>          Chromium distribution channel, "chrome", "chrome-beta", "msedge-dev", etc Chromium 分发渠道、“chrome”、“chrome-beta”、“msedge-dev”等
  --color-scheme <scheme>      emulate preferred color scheme, "light" or "dark" 模拟首选配色方案，“浅色”或“深色”
  --device <deviceName>        emulate device, for example  "iPhone 11" 模拟设备，例如“iPhone 11”
  --geolocation <coordinates>  specify geolocation coordinates, for example "37.819722,-122.478611"  指定地理位置坐标，例如“37.819722,-122.478611”
  --ignore-https-errors        ignore https errors 忽略 https 错误
  --load-storage <filename>    load context storage state from the file, previously saved with --save-storage 从文件加载上下文存储状态，之前使用 --save-storage 保存
  --lang <language>            specify language / locale, for example "en-GB"  指定语言区域，例如“en-GB”
  --proxy-server <proxy>       specify proxy server, for example "http://myproxy:3128" or "socks5://myproxy:8080" 指定代理服务器，例如“http:myproxy:3128”或“socks5:myproxy:8080”
  --save-storage <filename>    save context storage state at the end, for later use with --load-storage 最后保存上下文存储状态，以备后用 --load-storage
  --save-trace <filename>      record a trace for the session and save it to a file 记录会话的跟踪并将其保存到文件中
  --timezone <time zone>       time zone to emulate, for example "Europe/Rome" 要模拟的时区，例如“EuropeRome”
  --timeout <timeout>          timeout for Playwright actions in milliseconds (default: "10000") 剧作家动作的超时时间（以毫秒为单位）（默认值：“10000”）
  --user-agent <ua string>     specify user agent string 指定用户代理字符串
  --viewport-size <size>       specify browser viewport size in pixels, for example "1280, 720" 以像素为单位指定浏览器视口大小，例如“1280、720”
  -h, --help                   display help for command 显示命令帮助
"""


# dataclasses.dataclass(*, init=True, repr=True, eq=True, order=False, unsafe_hash=False, frozen=False)
@dataclass(order=True)  # 注意这里
class RecordScriptMain(object):
    url: str = field(default="https://www.baidu.com")
    output: str = field(default="NewCreatePython.py")
    target: str = field(default=" python")
    browser: str = field(default="chromium")
    channel: str = field(default="chrome")
    color_scheme: str = field(default="light")
    device: str = field(default=None)
    geolocation: str = field(default="37.819722,-122.478611")
    ignore_https_errors: bool = field(default=False)
    load_storage: str = field(default=None)
    lang: str = field(default="zh-CN")
    proxy_server: str = field(default=None)
    save_storage: str = field(default=None)
    save_trace: str = field(default=None)
    timezone: str = field(default="Asia/Shanghai")
    timeout: str = field(default="10000")
    user_agent: str = field(default=None)
    viewport_size: str = field(default="1280,720")
    def run_record_script(self):
        save_path = ['save_trace', 'load_storage', 'save_storage']
        annotations = self.__dict__
        init_cmd_dode = "playwright codegen "
        url = ''
        for r in annotations:
            if self.__dict__[r]:
                if r == 'url':
                    url = ' ' + self.__dict__[r]
                elif r == 'ignore_https_errors':
                    ignore_https_errors = ' ignore_https_errors'
                elif r in save_path:
                    init_cmd_dode += f'--{r}={self.__dict__[r]} '
                else:
                    init_cmd_dode += f'--{r} {self.__dict__[r]} '
        init_cmd_dode += url
        init_cmd_dode = init_cmd_dode.replace('_', '-')
        print(init_cmd_dode)
        os.system(init_cmd_dode)


if __name__ == '__main__':
    RecordScript = RecordScriptMain(url='http://jd.com/', save_storage='saveStorage.json',
                                    save_trace='save_trace.zip')
    RecordScript.run_record_script()
